/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
*
*  
* @author: jin Chen
* @date: Oct 12, 2004$
* @version: 1.0
*/
package edu.psu.geovista.app.pcp.plot.layer;

import edu.psu.geovista.app.pcp.plot.layer.event.LayerEvent;
import edu.psu.geovista.app.pcp.plot.layer.event.LayerListener;
import edu.psu.geovista.app.plot.layer.Layer;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.util.*;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

 /**/
 public class ImageCacheLayer extends ImageLayer implements LayerListener{
     public  static final boolean DEBUG=false;
     boolean imageOutofdate=true;
     List layers=new ArrayList();

     private static final Logger logger=Logger.getLogger(ImageCacheLayer.class.getName() );
     static {
         //set up logger
         Level mylevel= Level.INFO;
           ConsoleHandler handler=new ConsoleHandler();
           logger.addHandler(handler);  // 

           logger.setLevel(mylevel);
           handler.setLevel(mylevel );
     }


     public ImageCacheLayer(String name) {
           this.setName(name);
     }

     public void paintLayer(){
         logger.fine("ImageCacheLayer paint");
         long start=0,stop=0; //put is at start of method
         AffineTransform af = new AffineTransform();
         //if(this.isImageOutofdate() ){
             //this.clearLayer();
             Graphics2D g2d = this.getImagePainter();
             Collections.sort(layers);
             Iterator iter=layers.iterator() ;
             //boolean hasRangedata=false;
             while(iter.hasNext() ){

                 Layer layer=(Layer)iter.next() ;
                 //String name = layer.getName();
                 if (!layer.isVisible() ||!layer.isPersisted() ){//not visible or not persist --> not paint
                  continue;
              }
                 RenderedImage bimg=((ImageLayer)layer).getImage() ;
                 if (bimg!=null){
                     if (DEBUG) {//put is at start of method
                         start = Calendar.getInstance().getTime().getTime();
                     }
                     Composite old=g2d.getComposite() ;
                     if (layer.getComposite() !=null){
                         g2d.setComposite(layer.getComposite()) ;
                     }
                     //System.out.println("Draw image for layer:"+layer.getName() );   //AllData_LAYER="lnLyr";  ShowRangeData_LAYER="DsrLyr";

                     /*if(name.equals(Layer.AllData_LAYER)){
      continue; }              */
                     //System.out.println("Draw image for layer:"+layer.getName() + " layer composite:"+layer.getComposite());
                     g2d.drawRenderedImage(bimg, af);// 
                     g2d.setComposite(old);

                     if (DEBUG) {  //put is at end of method
                         stop = Calendar.getInstance().getTime().getTime();
                         System.out.println("final combine "+layer.getName()+" Image take " + (stop - start) * 0.001f + " to finish");
                     }
                 }

             }    //while
         //}//if
         this.setImageOutofdate(false);
     }
    public void layerChange(LayerEvent e) {
         this.setImageOutofdate(true);
     }
     public void addToCache(ImageLayer imglyr){
         layers.add(imglyr);

         imglyr.addLayerListener(this);

     }
     public boolean contain(Layer layer){
         return layers.contains(layer);
     }
     /*public void removeFromCache(ImageLayer imglyr){
     //layers.add(imglyr);
     }*/

     public boolean isImageOutofdate() {
         return imageOutofdate;
     }

     public void setImageOutofdate(boolean imageOutofdate) {
         this.imageOutofdate = imageOutofdate;
     }


     public BufferedImage getImage() {

         if (this.image ==null){
             this.image =BasicLayer.getBlankImage(this.getSize());
             return image;
         }
         if(this.isImageOutofdate() ){
             this.repaintLayer();
         }
         return image;
     }


 }
